package cn.edu.cqvie.jdk8.fun;

import java.util.function.BiFunction;
import java.util.function.Function;

public class FunctionTest2 {

    public static void main(String[] args) {
        FunctionTest2 test = new FunctionTest2();

//        System.out.println(test.compute(2, val -> val * 3, val -> val * val)); // 12
//        System.out.println(test.compute2(2, val -> val * 3, val -> val * val)); // 36

        System.out.println(test.compute3(1, 2 , (val1, val2) -> val1 + val2)); //3
        System.out.println(test.compute3(1, 2 , (val1, val2) -> val1 - val2)); //-1
        System.out.println(test.compute3(1, 2 , (val1, val2) -> val1 * val2)); //2
        System.out.println(test.compute3(1, 2 , (val1, val2) -> val1 / val2)); //0

        System.out.println(test.compute4(2,3 ,(val1,val2) -> val1 + val2, val -> val * val)); //25
    }

    public int compute(int a , Function<Integer, Integer> function1, Function<Integer,Integer> function2) {
        return function1.compose(function2).apply(a);
    }

    public int compute2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
        return function1.andThen(function2).apply(a);
    }

    public int compute3(int a, int b, BiFunction<Integer,Integer, Integer> biFunction) {
        return biFunction.apply(a,b);
    }

    public int compute4(int a, int b, BiFunction<Integer,Integer, Integer> biFunction,
                        Function<Integer, Integer> function) {
        return biFunction.andThen(function).apply(a,b);
    }
}
